<template>
  <div>
    <el-form v-loading="loadingBtn.diaSubmitBtn" ref="addForm"
             class="system-form" :label-width="formLabelWidth">
      <el-form-item label="内置账户">
        <div class="formItem">
          <el-select clearable multiple v-model="accountIdArr" placeholder="内置账户将无法修改/删除/禁用">
            <el-option v-for="item in accountArr" :key="item.id" :label="item.acc" :value="item.id"></el-option>
          </el-select>
          <el-button icon="el-icon-finished" :loading="loadingBtn.diaSubmitBtn" type="primary"
                     @click="submitForm('addForm','initAccount')">保存
          </el-button>
        </div>
      </el-form-item>
      <el-form-item label="内置角色">
        <div class="formItem">
          <el-select clearable multiple v-model="initRoleIdArr" placeholder="内置角色将无法修改/删除/禁用">
            <el-option v-for="item in roleArr" :key="item.id" :label="item.appellation" :value="item.id"></el-option>
          </el-select>
          <el-button icon="el-icon-finished" :loading="loadingBtn.diaSubmitBtn" type="primary"
                     @click="submitForm('addForm','initRole')">保存
          </el-button>
        </div>
      </el-form-item>
      <el-form-item label="默认角色">
        <div class="formItem">
          <el-select clearable multiple v-model="defRoleIdArr" placeholder="默认角色为账户初始角色">
            <el-option v-for="item in roleArr" :key="item.id" :label="item.appellation" :value="item.id"></el-option>
          </el-select>
          <el-button icon="el-icon-finished" :loading="loadingBtn.diaSubmitBtn" type="primary"
                     @click="submitForm('addForm','defRole')">保存
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { queryAll as queryAllRole, setInitRole, setDefaultRole, listDefaultRole, listInitRole } from '@/api/system/role'
import { queryAll as queryAllAccount, setInitAccount, listInitAccount } from '@/api/system/account'

export default {
  data () {
    return {
      loadingBtn: {
        diaSubmitBtn: false
      },
      roleArr: [],
      accountArr: [],
      initRoleIdArr: [],
      defRoleIdArr: [],
      accountIdArr: [],
      // 表单标签宽度
      formLabelWidth: '100px'
    }
  },
  methods: {
    /**
     * 提交按钮
     * @param formName
     */
    submitForm (formName, flag) {
      this.loadingBtn.diaSubmitBtn = true
      switch (flag) {
        case 'initAccount':
          setInitAccount(this.accountIdArr).then(({ code, msg }) => {
            if (code === this.$constant.SUCCESS) {
              this.$message.success(msg)
            }
            this.loadingBtn.diaSubmitBtn = false
          })
          break
        case 'initRole':
          setInitRole(this.initRoleIdArr).then(({ code, msg }) => {
            if (code === this.$constant.SUCCESS) {
              this.$message.success(msg)
            }
            this.loadingBtn.diaSubmitBtn = false
          })
          break
        case 'defRole':
          setDefaultRole(this.defRoleIdArr).then(({ code, msg }) => {
            if (code === this.$constant.SUCCESS) {
              this.$message.success(msg)
            }
            this.loadingBtn.diaSubmitBtn = false
          })
          break
        default:
          this.loadingBtn.diaSubmitBtn = false
      }
    },
    init () {
      queryAllRole().then(({ data, code }) => {
        if (code === this.$constant.SUCCESS) {
          this.roleArr = data
        }
      })
      queryAllAccount().then(({ data, code }) => {
        if (code === this.$constant.SUCCESS) {
          this.accountArr = data
        }
      })
      listInitAccount().then(({ data, code }) => {
        if (code === this.$constant.SUCCESS) {
          this.accountIdArr = data
        }
      })
      listDefaultRole().then(({ data, code }) => {
        if (code === this.$constant.SUCCESS) {
          this.defRoleIdArr = data
        }
      })
      listInitRole().then(({ data, code }) => {
        if (code === this.$constant.SUCCESS) {
          this.initRoleIdArr = data
        }
      })
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>
.formItem {
  display: flex;
  justify-content: space-between;
}

.el-select {
  width: 80%;
}
</style>
